/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.History;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import za.org.foobar.famon.frontend.client.dto.FailureDto;
import za.org.foobar.famon.frontend.client.framework.DateUtil;

import java.util.Date;

/**
* @author Carl Crous
*/
public class FailureListGrid extends ListGrid {
    private final SmartPortlet portlet;
    private DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy/MM/dd HH:mm:ss SSS");
    private DateTimeFormat timeFormat = DateTimeFormat.getFormat("HH:mm:ss SSS");
    public static final String COMPONENT_ID_PREFIX = "failureGrid";
    
    public FailureListGrid(final SmartPortlet portlet, String failureId) {
        setID(COMPONENT_ID_PREFIX+failureId);
        this.portlet = portlet;
        setDataSource(FailureDto.getDataSource(portlet));

        setAutoFetchData(true);
        setCanGroupBy(false);
        setCanMultiSort(true);
        setWidth100();
        setHeight(500);

        ListGridField id = new ListGridField("id");
        id.setHidden(true);
        
        ListGridField applicationGroup = new ListGridField("applicationGroupId", "Group");
        applicationGroup.setHidden(true);

        ListGridField date = new ListGridField("date", "Date");
        date.setCellFormatter(new CellFormatter() {
            public String format(Object o, ListGridRecord record, int rowNum, int colNum) {
                Date d = (Date)o;
                Date now = DateUtil.addDays(new Date(), -1);
                if (d.before(now)) {
                    return dateTimeFormat.format(d);
                } else {
                    return timeFormat.format(d);
                }
            }
        });
        date.setWidth(128);

        ListGridField failureType = new ListGridField("failureType", "Type");
        ListGridField message = new ListGridField("message", "Message");
        setFields(id, applicationGroup, date, failureType, message);

        addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ListGridRecord selectedRecord = getSelectedRecord();
                if (selectedRecord != null) {
                    String id = selectedRecord.getAttribute("id");
                    History.newItem("failure?id=" + id);
                }
            }
        });
    }
}
